import { OrderStatus } from '../order/config';
import { OrdersCount } from '../../services/order/order';
import Toast from 'tdesign-miniprogram/toast/index';
import util from "../../utils/util.js";

const menuData = [
  { title: '地址管理', tit: '', url: '/pages/usercenter/address/list/index', type: 'address', icon: 'dizhi' },
  { title: '发票抬头', tit: '', url: '/pages/order/receipt/index', type: 'address', icon: 'fapiao' },
  { title: '修改密码', tit: '', url: '/pages/usercenter/editPassword/index', type: 'address', icon: 'mima' },
  { title: '隐私条款', tit: '', url: '/pages/usercenter/agreement/policy/index', type: 'address', icon: "yinsi" },
  { title: '联系我们', tit: '', url: '', type: 'service', icon: 'lianxi', },
];

const orderTagInfos = [
  { tabType: OrderStatus.PENDING_DELIVERY, title: '待发货', iconName: 'daifahuo', status: 1, orderNum: 0, },
  { tabType: OrderStatus.PENDING_RECEIPT, title: '待收货', iconName: 'shenqingfahuo', status: 1, orderNum: 0, },
  { tabType: OrderStatus.PENDING_PAYMENT, title: '待支付', iconName: 'licai', status: 1, orderNum: 0, },
  { tabType: OrderStatus.COMPLETE, title: '已完成', iconName: 'yiwancheng', status: 1, orderNum: 0, },
];

const getDefaultData = () => ({
  userInfo: {},
  menuData,
  orderTagInfos,
  customerServiceInfo: {},
  currAuthStep: 1,
  versionNo: '',

  serviceVisible: false,  // 联系我们弹窗
  serviceInfo: { phone: '400 180 8358', emit: 'Chenqh@ydc360.com' }
});

Page({
  data: getDefaultData(),

  onLoad() {
    this.getVersionInfo();
  },

  onShow() {
    this.fetUseriInfoHandle();
  },
  onPullDownRefresh() {
    this.fetUseriInfoHandle();
  },

  /* 用户信息/订单统计 */
  fetUseriInfoHandle() {
    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo) this.setData({ userInfo: JSON.parse(userInfo), currAuthStep: 2, })
    OrdersCount({}).then(res => {
      if (res.code === '0000') {
        orderTagInfos[0].orderNum = res.data.waitShipmentsNum;
        orderTagInfos[1].orderNum = res.data.waitSignNum;
        orderTagInfos[2].orderNum = res.data.waitPayNum;
        this.setData({ orderTagInfos });
        wx.stopPullDownRefresh();
      }
    });
  },

  /* 监听入口 */
  onClickCell({ currentTarget }) {
    const { dataset } = currentTarget.dataset;
    if (dataset.type === 'service') {
      this.setData({ serviceVisible: true })
    } else {
      wx.navigateTo({ url: dataset.url });
    }
  },
  /* 进入订单列表 */
  jumpNav(e) {
    const tabType = e.detail.tabType;
    wx.navigateTo({ url: `/pages/order/order-list/index?status=${tabType}` });
  },
  /* 全部订单 */
  jumpAllOrder() {
    wx.navigateTo({ url: '/pages/order/order-list/index' });
  },
  /* 拨打电话 */
  call() {
    wx.makePhoneCall({
      phoneNumber: this.data.serviceInfo.phone
    });
  },
  /* 复制邮箱 */
  copyEmit() {
    wx.setClipboardData({
      data: this.data.serviceInfo.emit,
      success: function () {
        util.showToast('复制成功');
      }
    })
  },
  /* 关闭联系我们 */
  closesService() {
    this.setData({ serviceVisible: false })
  },
  /* 退出登录 */
  logOut() {
    wx.showModal({
      title: "",
      content: "是否确认退出登录",
      showCancel: true,
      success: function (cif) {
        if (cif.confirm) {
          wx.navigateTo({ url: "/pages/login/index" });
        }
      }
    });
  },

  gotoUserEditPage() {
    const { currAuthStep } = this.data;
    if (currAuthStep === 2) {
      wx.navigateTo({ url: '/pages/usercenter/person-info/index' });
    } else {
      this.fetUseriInfoHandle();
    }
  },

  getVersionInfo() {
    const versionInfo = wx.getAccountInfoSync();
    const { version, envVersion = __wxConfig } = versionInfo.miniProgram;
    this.setData({
      versionNo: envVersion === 'release' ? version : envVersion,
    });
  },
});
